/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.knoppers.examples;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.util.LabelValueBean;

/**
 *
 */
public class InputAction extends DispatchAction {

	private final static String SUCCESS = "success";

	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward populate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		final InputForm inputForm = (InputForm) form;
		inputForm.setCountryList(createCountryList());
		inputForm.setStateList(createStateList());

		return mapping.findForward(SUCCESS);
	}

	/**
	 * @return
	 */
	private List<LabelValueBean> createStateList() {
		final List<LabelValueBean> stateList = new ArrayList<LabelValueBean>();
		stateList.add(new LabelValueBean("New York", "1"));
		stateList.add(new LabelValueBean("California", "2"));
		stateList.add(new LabelValueBean("Los Angeles", "3"));
		return stateList;
	}

	/**
	 * @return
	 */
	private List<CountryData> createCountryList() {
		final List<CountryData> countryList = new ArrayList<CountryData>();
		countryList.add(new CountryData("1", "USA"));
		countryList.add(new CountryData("2", "Canada"));
		countryList.add(new CountryData("3", "Mexico"));
		return countryList;
	}

	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		final InputForm inputForm = (InputForm) form;
		System.out.println(inputForm.getCountry());
		System.out.println(inputForm.getState());
		return mapping.findForward(SUCCESS);
	}
}